﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
using OpenTK;

namespace MyRTS
{
    public partial class Form1 : Form
    {
        bool loaded = false;
        float rotation1 = 0;
        Stopwatch sw = new Stopwatch(); // available to all event handlers
        private void glControl1_Load(object sender, EventArgs e)
        {
            loaded = true;
          //  GL.ClearColor(Color.SkyBlue); // Yey! .NET Colors can be used directly!
            CreateShaders();
            GL.Viewport(0, 0, glControl1.Width, glControl1.Height);

            GL.Enable(EnableCap.DepthTest);
            GL.ClearColor(System.Drawing.Color.MidnightBlue);

   
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            if (!loaded) // Play nice
                return;

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
         
            RenderVBO();


            glControl1.SwapBuffers();
        }
        Matrix4 per;
        Matrix4 lookat;

        private void SetupViewport()
        {
            int w = glControl1.Width;
            int h = glControl1.Height;

            float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] mat_shininess = { 50.0f };
            float[] light_position = { 10.0f, -100.0f, 10.0f, 0.0f };
            float[] light_ambient = { 0.5f, 0.5f, 0.5f, 1.0f };

            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.ShadeModel(ShadingModel.Smooth);

            GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular);
            GL.Material(MaterialFace.Front,  MaterialParameter.Shininess, mat_shininess);
            GL.Light( LightName.Light0,  LightParameter.Position, light_position);
            GL.Light( LightName.Light0,  LightParameter.Ambient, light_ambient);
            GL.Light( LightName.Light0,  LightParameter.Diffuse, mat_specular);

            GL.Enable( EnableCap.Lighting);
            GL.Enable( EnableCap.Light0);
            GL.Enable( EnableCap.DepthTest);
            GL.Enable( EnableCap.ColorMaterial);
            GL.Enable( EnableCap.CullFace);


            GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
          
            GL.LoadIdentity();

            float aspect = (float)((float)w / (float)h);
          // GL.Ortho(0, w, 0, h, -1000, 1000); // Bottom-left corner pixel has coordinate (0, 0)
            per=Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect, 1f, 1000f);
          //  GL.
            GL.LoadMatrix(ref per);
            GL.MatrixMode(MatrixMode.Projection);
            
        }

        private void glControl1_Resize(object sender, EventArgs e)
        {
            GL.Viewport(0, 0,glControl1.Width,glControl1.Height);
          //  SetupViewport();
        }

    }
}
